package com.example.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dao.MenuMapper;
import com.example.dao.OrderUserMapper;
import com.example.model.Menu;
import com.example.model.OrderDish;
import com.example.dao.OrderDishMapper;
import com.example.model.OrderDishHelper;
import com.example.model.OrderUser;
import com.example.service.IOrderDishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Hello
 * @since 2024-07-03
 */
@Service
public class OrderDishServiceImpl extends ServiceImpl<OrderDishMapper, OrderDish> implements IOrderDishService {
    @Autowired
    private OrderUserMapper orderUserMapper; // Mapper for OrderUser

    @Autowired
    private OrderDishMapper orderDishMapper; // Mapper for OrderDish
    @Autowired
    MenuMapper menuMapper;

    @Transactional
    @Override
    public void createOrder(OrderUser orderUser, List<OrderDishHelper> orderDishes) {
        // Generate a UUID for the order ID
        String orderId = UUID.randomUUID().toString();
        orderUser.setOrderId(orderId);

        // Insert the order user data
        orderUserMapper.insert(orderUser);

        // Insert each order dish with the generated order ID
        for (OrderDishHelper orderDishHelper : orderDishes) {
            QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
            menuQueryWrapper.eq("restaurant_id",orderDishHelper.getRestaurantId()).eq("dish_name",orderDishHelper.getDishName());
            Menu menu = menuMapper.selectOne(menuQueryWrapper);
            Integer dishId = menu.getDishId();
            OrderDish orderDish = new OrderDish();
            orderDish.setOrderId(orderId);
            orderDish.setDishQuantity(orderDishHelper.getDishQuantity());
            orderDish.setDishId(dishId);
            orderDish.setIsCompleted(orderDishHelper.getIsCompleted());
            orderDish.setTableNumber(orderDishHelper.getTableNumber());
            orderDishMapper.insert(orderDish);
        }
    }
}
